package org.hk.kg.finance.stuclz.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hk.kg.db.DBContext;
import org.hk.kg.ui.KGUiContext;
import org.hk.kg.utils.Constants;

public class StuClzService implements IStuClzService{
	private static Log log = LogFactory.getLog(StuClzService.class);
	
	private static final String CREATE_STU_CLZ_SQL = "insert into stu_clz(stuClzName,stuClzType)values('%s','%s')",
		DELETE_STU_CLZ_SQL = "delete from stu_clz where stuClzId=%s",
		GET_STU_CLZ_BYID_SQL = "select stuClzId,stuClzName,stuClzType from stu_clz where stuClzId=%d",
		UPDATE_STU_CLZ_SQL = "update stu_clz set stuClzName='%s',stuClzType='%s' where stuClzId=%d";
	public List query()
	{
		List<Map> rlt = DBContext.instatnce.query(Constants.DB_DEFAULT, "select stuClzId,stuClzName,stuClzType from stu_clz");
		return rlt;
	}
	public boolean createStuClz(String clzName, String clzType) {
		int rlt = DBContext.instatnce.execute(Constants.DB_DEFAULT,String.format(CREATE_STU_CLZ_SQL, clzName,clzType));
		return rlt == 1;
	}
	
	public boolean deleteStuClasses(Object[] clzIds) {
		String[] sqls = new String[clzIds.length];
		for(int clzIdIdx = 0 ; clzIdIdx < clzIds.length ;clzIdIdx++)
		{
			sqls[clzIdIdx] = String.format(DELETE_STU_CLZ_SQL, clzIds[clzIdIdx]);
		}
		
		return DBContext.instatnce.batchExecute(Constants.DB_DEFAULT, sqls) == 1;
	}
	
	public Map getStuClzById(int clzId) {
		String sql = String.format(GET_STU_CLZ_BYID_SQL, clzId);
		return DBContext.instatnce.querySingle(Constants.DB_DEFAULT, sql);
	}
	public boolean modifyStuClz(int clzId, String clzName, String clzType) {
		String sql = String.format(UPDATE_STU_CLZ_SQL, clzName,clzType,clzId);
		return DBContext.instatnce.execute(Constants.DB_DEFAULT, sql)==1;
	}
}
